package rankingGenerator;

import configuration.parameters.DoubleValuedParameter;
import configuration.parameters.Parameter;
import rankingGenerator.statistics.DREComparableStatistic;
import rankingGenerator.statistics.DoubleStatisticValue;
import distributedRE.model.DataBase;
import distributedRE.model.DistributedREModel;
import distributedRE.model.Query;
import distributedRE.model.QueryDatabase;
import distributedRE.model.Session;
import estimators.numberoOfDocuments.DocumentEstimator;

public class MeanUsefulValue extends RankingGenerator {

	private DocumentEstimator de;

	public MeanUsefulValue(DocumentEstimator de){
		this.de = de;
	}
	
	@Override
	public DREComparableStatistic calculateCostValue(Query query,
			DataBase dataBase, DistributedREModel dREModel, Session s) {
		
		QueryDatabase qdb = QueryDatabase.getInstance(query, dataBase, dREModel);
		
		double precision = LastPrecision(qdb);
		
		long sample = dataBase.getMaxRetrievable();
		
		double retrieved = RetrievedBy(qdb);
		
		return new DoubleStatisticValue(Math.min(sample, retrieved)*precision);
	
	}

	private double RetrievedBy(QueryDatabase qdb) {
		
		Parameter nod; 
			
		double number = de.estimateDocumentsRetrieved(qdb);
		
		nod = new DoubleValuedParameter(number);
		
		qdb.addParameter(Parameter.NUMBEROFDOCUMENTS, nod);
		
		return ((Double)nod.getValue()).doubleValue();

	}

	private double LastPrecision(QueryDatabase qdb) {

		Parameter p = qdb.getParameter(Parameter.PRECISION);
		
		return ((Double)p.getValue()).doubleValue();
		
	}
	
	public String toString(){
		return "Mean Useful Value";
	}
	
}
